#!/bin/bash
# The set of the infrastructure API of the CloudSystem can be found in the package: infrastructureAPI/script/.
# This script produces the complete set of all found programs in the infrastructureAPI/script package.
# After the execution of the createSHScripts the user is able to launch each API from the command line.
# All produced scripts can be found in the destination directory pointed by the variable: DEST_DIR.

# Follows an example of the command line needed to launch the java program of the run-nodes belonging to the API set:

# Example command line:
# $java -cp `pwd`/bin -Djava.rmi.server.codebase=file://`pwd`/deploy/cloudsystemNode.jar -Djava.security.policy=file://`pwd`/security.policy infrastructureAPI.scripts.instanceTools.run_nodes -node node3### pippo 4

# After the execution of the createSHScript.sh the user has only to position the console into the DEST_DIR directory and then simply use the API "as they are". For example now to launch the run-nodes we have to type only:
# $run-nodes -node node3### pippo 4
# where only the parameters of the script are required.

BIN="bin"
CLASS=".class"
INFRASTRUCTURE_DIR="infrastructureAPI/scripts"
CLASS_TARGET="$BIN""/""$INFRASTRUCTURE_DIR"
DEST_DIR="API-scripts"
CLASSPATH=`pwd`"/bin/"

#Assumption: all directories end with the "Tool" word
DIRS=`ls $CLASS_TARGET | grep Tools`
FILES=`find $CLASS_TARGET/**/ -name *$CLASS`

SCRIPT_HEADER="#!/bin/bash"
SCRIPT_COMMENT="#AUTO-Generated script file"

RMI_CODEBASE_PARAMETER="-Djava.rmi.server.codebase=file://`pwd`/deploy/cloudsystemNode.jar"
RMI_SECURITY_PARAMETER="-Djava.security.policy=file://`pwd`/security.policy"
RMI_HOSTNAME_PARAMETER="-Djava.rmi.server.hostname=`hostname`"

JAVA_COMMAND="java -cp $CLASSPATH $RMI_CODEBASE_PARAMETER  $RMI_SECURITY_PARAMETER "
JAVA_PARAMETERS="\$@"

# We have to convert
# each class file into the java string that will
# be passed to the java command. 
# For example, supposing that
# we have to type the command:
#		$java -cp `pwd`/bin infrastructureAPI.instanceTools.run_nodes
# the class file found at
#	   bin/infrastructureAPI/instanceTools/run_nodes.class
# must be transformed into:
#	 	infrastructureAPI.instanceTools.run_nodes
# and then we have to extract the new script name. 
# Following the example will be: 
#	 	run-nodes.sh
echo "Script creation...
		processing: "
for i in $FILES
do	
#	echo "$i"
	STR=`echo ${i#$BIN/}`
	STR=`echo ${STR/$CLASS/}`
	STR=`echo ${STR/\//.}`
	JAVA_TARGET=`echo ${STR/\//.}`
	
#	echo $JAVA_TARGET
	
	SCRIPT_NAME=`echo ${STR##*\/}`
	SCRIPT_NAME=`echo ${SCRIPT_NAME/_/-}`".sh"
	
#	echo $SCRIPT_NAME
	
	TARGET_FILE=$DEST_DIR/$SCRIPT_NAME

	echo $TARGET_FILE
	echo "" > $TARGET_FILE
	echo $SCRIPT_HEADER >> $TARGET_FILE
	echo $SCRIPT_COMMENT >> $TARGET_FILE
	echo $JAVA_COMMAND $JAVA_TARGET $JAVA_PARAMETERS >> $TARGET_FILE
	chmod +x $TARGET_FILE
		
done

exit 0

